/*
 * CS_APPLY_SKILL_POINT.cpp
 *
 *  Created on: 2013. 5. 21.
 *      Author: the2
 */

#include "../../User.h"
#include "CS_APPLY_SKILL_POINT.h"

#include <vector>

namespace ProjectZ { namespace NCommand { namespace NAbility {

void CS_APPLY_SKILL_POINT::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * // 스킬 적용 요청
	CS_APPLY_SKILL_POINT		0x0923
	{
		u8	skill_cnt	max 20			/// 최대 스킬 갯수 만큼 반복구
		{
			u8	add_skill_point;
		}
	}

	//
	SC_APPLY_SKILL_POINT		0x0924
	{
		s16 ack;			/// 1:완료 ,  -1 실패
	}
	 */

	PREPARE_PACKET;

	uint8_t skill_cnt = 0;

	* pPacket >> skill_cnt;

	if (20 != skill_cnt)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->MsgCat("%2d", ACK_UNKNOWN_ERROR);
		pAck->MakePacket(CMD_SC_APPLY_SKILL_POINT);
		pUser->Send(pAck);
		return;
	}

	uint32_t totalUseCount = 0;
	std::vector<uint8_t> clsSkillPointVector;

	for( int i=0 ; i < skill_cnt ; i++ )
	{
		uint8_t add_skill_point = 0;
		* pPacket >> add_skill_point;

		totalUseCount += add_skill_point;
		clsSkillPointVector.push_back(add_skill_point);
	}

	if (totalUseCount > pUser->GetCharacterInfo().remain_skill_point)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->MsgCat("%2d", ACK_UNKNOWN_ERROR);
		pAck->MakePacket(CMD_SC_APPLY_SKILL_POINT);
		pUser->Send(pAck);
		return;
	}

	for( int i=0 ; i<skill_cnt ; i++ )
	{
		pUser->GetCharacterInfo().array_Skill[i] += clsSkillPointVector[i];
	}
	pUser->GetCharacterInfo().remain_skill_point -= totalUseCount;

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	pAck->MsgCat("%2d", ACK_OK);
	pAck->MakePacket(CMD_SC_APPLY_SKILL_POINT);
	pUser->Send(pAck);
}


} /*NAbility*/ } /*NCommand*/ } /*ProjectZ*/

